package com.jiawa.wiki.job;

import com.jiawa.wiki.mapper.EbookSnapshotCustom;
import com.jiawa.wiki.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 *
 * 电子书快照表内容更新（点赞，阅读，新增点赞，新增阅读数）
 *
 */
@Component
public class EbookSnapshotJob {

    @Resource
    private EbookSnapshotCustom ebookSnapshotCustom;

    @Resource
    private SnowFlake snowFlake;

    private static final Logger LOG = LoggerFactory.getLogger(EbookSnapshotJob.class);

    /**
     * 自定义cron表达式
     * 每30秒执行一次
     */
    @Scheduled(cron = "*/30 * * * * ?")
    public void updateSnapshot() {
        // 增加日志流水号，也叫记录号
        MDC.put("LOG_ID", String.valueOf(snowFlake.nextId()));
        LOG.info("开始更新电子书快照表（30秒/次）");
        long start = System.currentTimeMillis();
        ebookSnapshotCustom.updateSnapshot();
        LOG.info("更新快照表完成，耗时：{}ms", System.currentTimeMillis() - start);
    }

}
